package com.example.daxiguaforharmony.slice;

import com.example.daxiguaforharmony.MainAbility;
import com.example.daxiguaforharmony.ResourceTable;
import com.example.daxiguaforharmony.utils.DbKvManager;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;

/**
 * Full screen ability slice
 */
public class TestAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        // hide status bar
        this.getWindow().addFlags(WindowManager.LayoutConfig.MARK_ALLOW_EXTEND_LAYOUT);
        this.getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);

        this.setUIContent(ResourceTable.Layout_port_main);

        Button btnOk = (Button) findComponentById(ResourceTable.Id_btnOk);
        btnOk.setText("跳转");

        btnOk.setClickedListener(l -> {
            //Operation localOperation = new Intent.OperationBuilder()
//                    .withDeviceId("")
//                    .withBundleName("com.example.daxiguaforharmony")
//                    .withAbilityName("com.example.daxiguaforharmony.MainAbility")
//                    .withFlags(Intent.FLAG_START_FOREGROUND_ABILITY)
//                    .build();
//            Intent localIntent = new Intent();
//            localIntent.setOperation(localOperation);
//            IntentParams params = new IntentParams();
//            params.setParam("score", 101);
//            localIntent.replaceParams(params);
//
//            present(new MainAbilitySlice(), localIntent);
            DbKvManager dbKvManager = new DbKvManager(this.getApplicationContext());
            int score = 1234;
            System.out.println("游戏结束======"+score);
            Intent intent2 = new Intent();

            Operation operation =
                    new Intent.OperationBuilder()
                            .withBundleName(getBundleName())
                            .withAbilityName(MainAbility.class.getName())
                            .withAction("action.results")
                            .build();
            intent2.setOperation(operation);
//        IntentParams params = new IntentParams();
//        params.setParam("score", score);
//        intent.replaceParams(params);

            //DbKvManager dbKvManager = new DbKvManager(this.getContext());
            String score1 = String.valueOf(score);
            if(score1.isEmpty())
            {
                score1 = "0";
            }
            System.out.println("得分2======="+score1);
            String score_high = dbKvManager.read("score_high");
            score_high = String.valueOf(score_high);
            score_high = score_high.equals("0") ?"":score_high;
            score_high = score_high.isEmpty() ?score1:score_high;
            System.out.println("最高得分2======="+score_high);


            intent2.setParam(ResultsAbilitySlice.INTENT_TITLE, "大西瓜");
            intent2.setParam(ResultsAbilitySlice.INTENT_READ, score1);
            intent2.setParam(ResultsAbilitySlice.INTENT_LIKE, score_high);
            intent2.setParam(ResultsAbilitySlice.INTENT_CONTENT, score);
            intent2.setParam("score", score);

            //present(new ResultsAbilitySlice(), intent);
            startAbility(intent2);

            //dbKvManager = new DbKvManager(this.getApplicationContext());
            //dbKvManager.init(this);
            //dbKvManager.write("score",Integer.toString(score));

            //String deviceId = dbKvManager.getDeviceId();//获取当前设备id
            //dbKvManager.write("userscore_"+deviceId,Integer.toString(score));
            //dbKvManager.syncDeviceData();//多设备同步数据

            System.out.println("游戏结束end======"+score);
            terminate();//销毁窗体
        });
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
